#include "utils/RandomGenerator.h"
#include <QDateTime>

RandomGenerator::RandomGenerator()
    : m_generator(QRandomGenerator::global())
{
}

RandomGenerator::~RandomGenerator() = default;

QString RandomGenerator::generateString(int length, bool useUpperCase, bool useLowerCase, bool useNumbers)
{
    QString chars;
    
    if (useUpperCase) {
        chars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    }
    if (useLowerCase) {
        chars += "abcdefghijklmnopqrstuvwxyz";
    }
    if (useNumbers) {
        chars += "0123456789";
    }
    
    if (chars.isEmpty()) {
        return QString();
    }
    
    QString result;
    for (int i = 0; i < length; ++i) {
        int index = m_generator->bounded(chars.length());
        result.append(chars[index]);
    }
    
    return result;
}

QString RandomGenerator::generateIdentifier(int length)
{
    // 标识符必须以字母开头
    QString result = generateString(1, true, true, false);
    
    if (length > 1) {
        result += generateString(length - 1, true, true, true);
    }
    
    return result;
}

int RandomGenerator::generateInt(int min, int max)
{
    return m_generator->bounded(min, max + 1);
}

double RandomGenerator::generateDouble(double min, double max)
{
    return min + (max - min) * m_generator->generateDouble();
}